/**
 * 
 */
package co.gov.medellin.sdep.sisdep.cco.service;

import java.util.Date;
import java.util.List;

import co.gov.medellin.sdep.sisdep.cco.exception.LocalException;
import co.gov.medellin.sdep.sisdep.cco.exception.LocalRuntimeException;
import co.gov.medellin.sdep.sisdep.cco.exception.SolicitudLocalCambioException;
import co.gov.medellin.sdep.sisdep.cco.exception.SolicitudLocalRuntimeException;
import co.gov.medellin.sdep.sisdep.cco.exception.descriptor.LocalMessageDescriptor;
import co.gov.medellin.sdep.sisdep.cco.exception.descriptor.SolicitudLocalMessageDescriptor;
import co.gov.medellin.sdep.sisdep.common.message.MessageDescriptorConstant;
import co.gov.medellin.sdep.sisdep.common.service.BaseService;
import co.gov.medellin.sdep.sisdep.model.Local;
import co.gov.medellin.sdep.sisdep.model.dao.impl.ILocalDAOT;

/**
 * @author Usuario
 *
 */
public class LocalServiceImpl extends BaseService implements ILocalService {

	//----------------------------------------------------------------------
	//--- propiedades y constructores                                 init()
	//----------------------------------------------------------------------

	/**
	 * default serial version
	 */
	private static final long serialVersionUID = 1L;
	/** */
	private ILocalDAOT localDAO;

	/**
	 * 
	 */
	public LocalServiceImpl() {
	}

	//----------------------------------------------------------------------
	//--- propiedades y constructores                                  end()
	//----------------------------------------------------------------------

	//----------------------------------------------------------------------
	//--- implementacion de metodos para acciones                     init()
	//----------------------------------------------------------------------

	/**
	 * 
	 */
	public void insert(Local local) throws LocalException {
		try{
			local.setUsuarioCrea(getPrincipalUser());
			local.setFechaCrea(new Date());
			getLocalDAO().insert(local);
		}catch (Exception e) {
			Object [] infoError = {
			};
			if(e.getCause().toString().indexOf("UNIQUE KEY") >= 0){
				throw new LocalException(new LocalMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			}else{
				throw new LocalException(new LocalMessageDescriptor(MessageDescriptorConstant.SAVE), infoError, e);
			}
		}
	}

	/**
	 * 
	 */
	public final void update(Local local) throws LocalException {
		try{
			local.setUsuarioModifica(getPrincipalUser());
			local.setFechaModifica(new Date());
			getLocalDAO().update(local);
		}catch (Exception e) {
			Object [] infoError = {
			};
			if(e.getCause().toString().indexOf("UNIQUE KEY") >= 0){
				throw new LocalRuntimeException(new LocalMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			}else{
				throw new LocalRuntimeException(new LocalMessageDescriptor(MessageDescriptorConstant.UPDATE), infoError, e);
			}
		}
	}

	/**
	 * 
	 */
	public final void delete(Local local) throws LocalException {
		try {
			local.setUsuarioModifica(getPrincipalUser());
			local.setFechaModifica(new Date());
			getLocalDAO().delete(local);
		}catch (Exception e) {
			Object [] infoError = {
			};
			throw new LocalRuntimeException(new LocalMessageDescriptor(MessageDescriptorConstant.DELETE), infoError, e);
		}
	}

	/**
	 * @return 
	 * 
	 */
	public final List load(int start,int max, Local local) throws LocalException {
		try{
			//return getLocalDAO().getAllLocalPaginated(start, max, criteria);
			return getLocalDAO().load();
		}catch (Exception e) {
			Object [] infoError = null;
			throw new LocalRuntimeException(new LocalMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}
	

	/**
	 * @return 
	 * 
	 */
	public final List loadByNumeroLocal(int start,int max, Local local) throws LocalException {
		try{
			//return getLocalDAO().getAllLocalPaginated(start, max, criteria);
			return getLocalDAO().loadByNumeroLocal(local);
		}catch (Exception e) {
			Object [] infoError = null;
			throw new LocalRuntimeException(new LocalMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}	

	/**
	 * @return 
	 * 
	 */
	public final List listaLocales() throws LocalException {
		try{
			//return getSolicitudLocalDAO().getAllSolicitudLocalPaginated(start, max, criteria);
			return getLocalDAO().listaLocales();
		}catch (Exception e) {
			Object [] infoError = null;
			throw new SolicitudLocalRuntimeException(new SolicitudLocalMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}
	
	/**
	 * @return 
	 * 
	 */
	public final List listaLocalesPorCCO(Long idCentroComercial) throws LocalException {
		try{
			//return getSolicitudLocalDAO().getAllSolicitudLocalPaginated(start, max, criteria);
			return getLocalDAO().listaLocalesPorCCO(idCentroComercial);
		}catch (Exception e) {
			Object [] infoError = null;
			throw new SolicitudLocalRuntimeException(new SolicitudLocalMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}
		
	/**
	 * 
	 */
	public final Local get(long id) throws LocalException {
		try{
			return getLocalDAO().findByPrimaryKey(id);
		}catch (Exception e) {
			Object [] infoError = {new Long(id)};
			throw new LocalRuntimeException(new LocalMessageDescriptor(MessageDescriptorConstant.GET), infoError, e);
		}
	}

	//----------------------------------------------------------------------
	//--- implementacion de metodos para acciones                      end()
	//----------------------------------------------------------------------

	//----------------------------------------------------------------------
	//--- getters and setters                                         init()
	//----------------------------------------------------------------------

	/**
	 * @author Usuario
	 *
	 */
	public ILocalDAOT getLocalDAO() {
		return localDAO;
	}

	/**
	 * @author Usuario
	 *
	 */
	public void setLocalDAO(ILocalDAOT localDAO) {
		this.localDAO = localDAO;
	}

	//----------------------------------------------------------------------
	//--- getters and setters                                          end()
	//----------------------------------------------------------------------

}
